﻿@using hc.epm.Common;
@using hc.Plat.Common.Extend;
@{
    ViewBag.Title = "新增模型";
}
<body>
    <div class="contentData">
        <form id="mainForm">
            <div class="tab">
                <div class="long-text ProjectName">
                    <label for="ProjectName"><span class="star">*</span>&nbsp;项目名称&nbsp;:</label>
                    <input type="text" value="@ViewBag.ProjectName" name="ProjectName" id="ProjectName" readonly />
                    <input type="hidden" value="@ViewBag.ProjectId" id="ProjectId" name="ProjectId" />
                    <input type="hidden" id="BIMState" name="BIMState" />
                    <input type="hidden" id="BIMName" name="BIMName" />
                    <i class="pic iconfont" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
                </div>
                <div class="long-text">
                    <label for="Name"><span class="star">*</span>&nbsp;模型名称&nbsp;:</label>
                    <input type="text" name="Name" id="Name" autocomplete="off" />
                </div>
                <div class="intro">
                    <div><label for="Desciption"><span class="star">*</span>&nbsp;描述&nbsp;:</label></div>
                    <div>
                        <textarea class="sub-description" name="Desciption" id="Desciption"></textarea>
                    </div>
                </div>
                <div class="short-text" style="margin-top: 30px;">
                    <label><span class="star">*</span>&nbsp;版本标识&nbsp;:</label>
                    @Html.DropDownList("VersionNo", null, new { @class = "form-control selectpicker  show-tick" })
                    <input type="hidden" name="VersionName" id="VersionName" />
                </div>
                <div class="short-text">
                    <label for="VersionNo"><span class="star"></span>&nbsp;版本号&nbsp;:</label>
                    <input type="text" name="VersionOrder" id="VersionOrder" autocomplete="off" value="" />
                </div>
                <div class="intro">
                    <div><label for="Remark">&nbsp;备注&nbsp;:</label></div>
                    <div>
                        <textarea class="sub-description" name="Remark" id="Remark"></textarea>
                    </div>
                </div>
                <div class="long-text user" style="display:none;">
                    <label for="SubmitUserName"><span class="star">*</span>&nbsp;上传人&nbsp;:</label>
                    <input type="text" id="SubmitUserName" name="SubmitUserName" value="@ViewBag.UserName" readonly style="background-color:#EBEBE4" />
                    <input type="hidden" value="@ViewBag.UserID" id="SubmitUserId" name="SubmitUserId" />
                    <i class="pic iconfont ee" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
                </div>
                <div class="long-text unit" style="display:none;">
                    <label for="SubmitCompanyName"><span class="star">*</span>&nbsp;上传单位&nbsp;:</label>
                    <input type="text" id="SubmitCompanyName" name="SubmitCompanyName" value="@ViewBag.CompanyName" readonly style="background-color:#EBEBE4" />
                    <input type="hidden" value="@ViewBag.CompanyId" id="SubmitCompanyId" name="SubmitCompanyId" />
                    <i class="pic iconfont ff" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
                </div>
                <div class="short-text" style="display:none;">
                    <label for="SubmitDate"><span class="star">*</span>&nbsp;上传时间&nbsp;:</label>
                    <input type="text" id="SubmitDate" name="SubmitDate" value="" readonly style="background-color:#EBEBE4">
                    <i class="pic iconfont" width="20" height="30" style="cursor:pointer">&#xe7e2;</i>
                </div>
                <div class="upload" style="margin-bottom:0px;">
                    <div><label><span class="star">*</span>&nbsp;模型上传&nbsp;:</label></div>
                    <div class="btn-upload">
                        <button id="btnUploadFile" style="position: relative; z-index: 1;">上传模型</button>
                        <span style="vertical-align:bottom; margin-left:15px; color:red;">注意:将rvt文件压缩成*.rar(需要放在同名文件夹内)</span>
                    </div>
                </div>
                <div class="upload" style="margin-bottom:0px;">
                    <div><label></label></div>
                    <div style="width:100%;position:relative" id="BimfileList">
                        <ul></ul>
                        <div style="top:45%;left:35%;display:none;color:red" id="mx">
                            <h3 style="font-size:24px">模型正在轻量化中...</h3>
                        </div>
                    </div>
                </div>
                <div class="layui-block col-sm-5" style="margin-bottom:0px; float:none;">
                    <div style="width:80%;margin:10px auto 0px 110px; display:none;" id="fileBar">
                        <div class="layui-progress" lay-showpercent="true" lay-filter="fileProgress"><div class="layui-progress-bar" lay-percent="0%" style="width: 0%;"><span class="layui-progress-text">NaN%</span></div></div>
                    </div>
                    <input type="hidden" name="fileDataJsonFile" id="fileDataJsonFile">
                </div>
                <div class="button-group btn-center">
                    <button type="button" class="saveBut" data-state="@(ApprovalState.WaitAppr.GetValue().ToString())">提交</button>
                    <button type="button" class="saveBut" data-state="@(ApprovalState.Enabled.GetValue().ToString())">保存</button>
                    <input type="hidden" name="State" id="State" />
                </div>
            </div>
        </form>
    </div>
    <script>
        $(function () {
            var VersionName = $('#VersionNo option:selected').text();
            $("#VersionName").val(VersionName);

            $("#VersionNo").change(function () {
                var VersionName = $('#VersionNo option:selected').text();
                $("#VersionName").val(VersionName);
            });

            //当前时间
            var time = new Date();
            var Year = time.getFullYear()
            var Month = time.getMonth() + 1
            Month = Month < 10 ? "0" + Month : Month
            var day = time.getDate()
            day = day < 10 ? "0" + day : day
            time = Year + "-" + Month + "-" + day;
            $("#SubmitDate").val(time)
            //表单验证
            $("#mainForm").validate({
                errorClass: "validaterror",
                validClass: "validatevalid",
                errorElement: "span",
                success: function (label) {
                    label.addClass("validatevalid").html("&nbsp;");
                },
                rules: {
                    ProjectName: {
                        required: true,
                    },
                    Name: {
                        required: true,
                        maxlength: 100
                    },
                    Desciption: {
                        required: true,
                    },
                    VersionNo: {
                        required: true,
                    },
                    SubmitDate: {
                        required: true,
                        date: true
                    },
                },
                messages: {
                    ProjectName: {
                        required: "模型名称不能为空",
                    },
                    Name: {
                        required: "模型名称不能为空",
                        maxlength: "模型名称长度不能大于100个字符"
                    },
                    Desciption: {
                        required: "描述不能为空"
                    },
                    VersionNo: {
                        required: "版本标识不能为空"
                    },
                    SubmitDate: {
                        required: "上传日期不能为空",
                        date: "日期格式错误"
                    }
                }
            })

        })
        layui.use(['layer', 'form', 'element', 'laypage'], function () {
            var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
            //数据提交
            $(".saveBut").click(function () {
                if ($("#mainForm").valid()) {
                    var state = $(this).data("state");
                    $("#State").val(state);
                    var fileDataJson = $('#fileDataJsonFile').val();
                    if (state == @ApprovalState.WaitAppr.GetValue().ToString() && fileDataJson.length == 0) {
                        layer.alert("请上传模型!", { icon: 2 });
                        return;
                    }
                    if (fileDataJson.length == 0) {
                        $("#BIMState").val("@BIMModelState.NotBIM.ToString()");
                    } else {
                        $("#BIMState").val("@BIMModelState.BIMLightWeight.ToString()");
                    }
                    var search = $("form").serialize();
                    $.post("Add", search, function (data) {
                        if (data.Flag) {
                            layer.msg("操作成功", { time: 1000, icon: 1 }, function () {
                                window.location.href = "/Bim/Index";
                            });
                        } else {
                            layer.alert(data.Message, { icon: 2 });
                        }
                    });
                }
            });

            //图标上传
            var fileDataArray = [];
            var uploaderFile = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4', // 这里是说用什么技术引擎
                url: "/UploadBim/UploadHB", // 服务端上传路径
                max_file_size: "500mb", // 文件上传最大限制。
                max_file_count: 10, //指示用户可以同时上传文件的最大数量
                chunk_size: 0, // 上传分块每块的大小，这个值小于服务器最大上传限制的值即可,0表示不启用分块。
                browse_button: 'btnUploadFile',//触发上传元素
                flash_swf_url: '/Resource/scripts/plupload_2_1_2/Moxie.swf',
                silverlight_xap_url: '/Resource/scripts/plupload_2_1_2/Moxie.xap',
                multi_selection: false,//	是否可以在文件浏览对话框中选择多个文件
                filters: {
                    mime_types: [
                        { title: "Doc files", extensions: "rar" },
                    ],
                    max_file_size: "500mb", //最大只能上传100mb的文件
                    prevent_duplicates: true //不允许选取重复文件
                },
                init: {
                    PostInit: function () { },
                    FilesAdded: function (uploaderFile, files) {
                        var item = files[files.length - 1];
                        $.post("/Files/GetFileName", { tableName: 'Epm_Bim', name : item.name }, function (data) {
                            var fname = "";
                            if (data.Data.length==0) {
                                $('#BimfileList ul').html("");
                                fileDataArray = [];
                                uploaderFile.start();
                                $("#mx").hide()
                            } else {
                                parent.layer.msg("模型已存在", { time: 1000, icon: 2 });
                                uploaderFile.removeFile(item);
                            }
                        });
                    },
                    UploadProgress: function (uploaderFile, file) {
                        $("#fileBar").show();
                        element.progress("fileProgress", file.percent + "%");
                    },
                    FileUploaded: function (uploaderFile, file, responseObject) {
                        var data = JSON.parse(responseObject.response);
                        if (data) {

                            var odiv = '<li>';
                            odiv += '<img src="/Resource/images/moxing.jpg" style="width: 800px;height: 350px;cursor: pointer;" />';
                            odiv += "</li>";

                            setTimeout(function () {
                                $("#fileBar").hide();
                                $('#BimfileList ul').append(odiv);
                                $("#mx").css("display", "block");
                                element.progress("fileProgress", "0%"); 1
                            }, 1500);

                            parent.layer.msg("附件上传成功", { time: 1000, icon: 1 });
                            uploaderFile.removeFile(file);

                            var obj = {};
                            obj.FileGuid = file.id;
                            obj.Name = data[0].Name;
                            obj.Size = data[0].Size;
                            obj.GuidId = data[0].GuidId;
                            obj.ImageType = data[0].ImageType;
                            obj.Group = data[0].ResponseObject.FDFS_GROUP;
                            obj.Url = data[0].ResponseObject.FDFS_NAME;

                            fileDataArray.push(obj);
                            fileDataJson = JSON.stringify(fileDataArray);
                            $("#fileDataJsonFile").val(fileDataJson);

                            $("#BIMName").val((data[0].Name).split('.')[0]);
                        }
                        uploaderFile.removeFile(file);
                    },
                    Error: function (up, err) {
                        layer.alert("错误：" + err.code + ": " + err.message);
                    }
                }
            });
            uploaderFile.init();

            //在建项目
            $(".ProjectName").click(function () {
                layer.open({
                    type: 2,
                    title: GetLayerTitle("选择在建项目"),
                    shadeClose: false, //点击遮罩关闭层
                    area: ['800px', '600px'],
                    content: '/Comm/SelectProject',
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var obj = iframeWin.getSelectData();
                        console.log(obj)
                        if (obj.flag) {
                            $("#ProjectName").val(obj.data[0].Name);
                            $("#ProjectId").val(obj.data[0].Id);

                            $.post("GetListByProjectId?projectId=" + obj.data[0].Id, function (data) {
                                document.getElementById('VersionNo').disabled = false;
                                if (data == 1) {
                                    $("#VersionNo").find("option[value='FirstVersion']").remove();
                                    var VersionName = $('#VersionNo option:selected').text();
                                    $("#VersionName").val(VersionName);
                                } else if (data == 2) {
                                    //$("#VersionNo").empty();
                                    $("#VersionNo").val('');
                                    $("#VersionName").val('');
                                    document.getElementById('VersionNo').disabled = true;
                                    layer.alert("已存在竣工版模型，不能再上传！");
                                }
                                parent.layer.close(index);//关闭窗口
                            });
                        } else {
                            parent.layer.msg(obj.msg, { time: 1000, icon: 2 });
                            return false;
                        }
                    },
                    btn2: function (index, layero) {
                        parent.layer.close(index);//关闭窗口
                    },
                    cancel: function (index, layero) {
                    },
                });
            });
        });
    </script>
</body>


